package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.library.bean.OrgDeptUserBean;
import com.xspace.android.xafcommon.util.HeadShowUtil;

/**
 * Created by liuchen on 2016/10/27.
 */

public class SearchResultAdaper extends BaseAdapter {

    private List<OrgDeptUserBean> dataList;

    private static Context mContext;

    public SearchResultAdaper(Context mContext, List<OrgDeptUserBean> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    @Override
    public boolean isEmpty() {
        return super.isEmpty();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrgDeptUserBean bean = getItem(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_org_user, null);
            viewHolder = new ViewHolder();
            viewHolder.findView(convertView);
            convertView.setTag(viewHolder);
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setData(bean);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return dataList.get(position).hashCode();
    }

    @Override
    public OrgDeptUserBean getItem(int position) {
        return dataList.get(position);
    }


    static class ViewHolder {
        ImageView headImg;
        TextView nameTxt;
        TextView orgName;
        TextView deptName;

        public void findView(View v) {
            this.headImg = (RoundAngleImageView) v.findViewById(R.id.imagview_header);
            this.nameTxt = (TextView) v.findViewById(R.id.textview_name);
            this.orgName = (TextView) v.findViewById(R.id.txt_org_name);
            this.deptName = (TextView) v.findViewById(R.id.txt_dept_name);
        }

        public void setData(OrgDeptUserBean bean) {

            HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, this.headImg, HeadShowUtil.HeadType.UserLogo);

//            if (!TextUtils.isEmpty(bean.user_logo)) {
//                Picasso.with(mContext)
//                        .load(bean.user_logo)
//                        .placeholder(R.drawable.icon_header_default)
//                        .error(R.drawable.icon_header_default)
//                        .into(this.headImg);
//            } else {
//                this.headImg.setImageResource(R.drawable.icon_header_default);
//            }

            this.nameTxt.setText(bean.user_name);
            if (!TextUtils.isEmpty(bean.org_name)) {
                this.orgName.setVisibility(View.VISIBLE);
                this.orgName.setText(bean.org_name);
            }
            if (!TextUtils.isEmpty(bean.dept_name)) {
                this.deptName.setVisibility(View.VISIBLE);
                this.deptName.setText(bean.dept_name);
            }
        }
    }
}
